#pragma  once

#include <QGraphicsItem>
#include <QPainter>
using namespace std;
//#include "sim.h"
class Node;
class SceneView;
class Connector;
class Title;
class Pipe : public QObject, public QGraphicsLineItem
{
	Q_OBJECT
public:

	Pipe(const QPointF &_source, const QPointF &_dest);
	~Pipe();
	QRectF boundingRect() const;
	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
	QPointF source, dest;
	enum pipe_orientation_t{VERTICAL, HORIZONTAL, ZERO, NONE};
	Pipe::pipe_orientation_t orientation();
protected:
	void mousePressEvent(QGraphicsSceneMouseEvent *event);
	void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
	void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
	QVariant itemChange(GraphicsItemChange change, const QVariant &value);
private:
	bool movable;
signals:
	void positionChanged(Pipe *pipe);
	void selectionChanged(bool selected);
};

class PipeLine: public QObject, public QGraphicsItem
{
	Q_OBJECT
public:

	PipeLine(SceneView *_scene, Node *_source, Node *_dest, QString pipesCoordinates, QString _name);
	PipeLine(Node * sourceNode);
	~PipeLine();
	void adjust();
	QRectF boundingRect() const{return QRectF();};
	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){};
	bool isSelected();
	Title *title;
	Node *source, *dest;
	PropertiesModel* getModel(){ return model; }
	QString get_pipes_coordinate();
private:
	void add_pipes(const QString &coordinates);
	void resizeNext(int index, const QPointF &new_point);
	void resizePrevious(int index, const QPointF &new_point);
	Pipe* addPipe(int index, const QPointF &_source, const QPointF &_dest);
	void deletePipe(int index);
	void checkOrientation(int index);
	//void log(const QString &text){scene->main->log(text);}
	QString getDefaultName();
	SceneView *scene;
	
	vector<Pipe*> pipes;
	void init_model();
	PropertiesModel* model;

private slots:
	void userChangePosition(Pipe *pipe);
	void userChangeSelection(bool selected);
	void getModelData(const int &parent_row, const int &row, const QVariant &value);
	void updateProperties();

signals:
	void setModelData(const int &parent_row, const int &row, const QVariant &value);

};